{{ $current_page := . }}

{{/* Dynamically load menu for this docs page. */}}
{{/* Attempt to get menu name from `menu_name` param, Hugo's front matter menu config, or the last dir of filepath. */}}
{{ $menu_name := "" }}
{{ if .Params.menu | and (not (reflect.IsMap .Params.menu)) | and (not (reflect.IsSlice .Params.menu)) }}
  {{ $menu_name = .Params.menu }}
{{ else if .Params.menu | and (or (reflect.IsMap .Params.menu) (reflect.IsSlice .Params.menu)) }}
  {{ range $k, $v := .Params.menu }}
    {{ $menu_name = $k }}
  {{ end }}
{{ end }}
{{ $menu_name = .Params.menu_name | default $menu_name | default (path.Base (path.Split .File).Dir) }}
{{ if not (index site.Menus $menu_name) }}
  {{ errorf "Please define menu items named `menu: %s:` in your %s front matter or define `[[menu.%s]]` in `config/default/menus.toml`. See https://wowchemy.com/docs/managing-content/#menus" $menu_name .Path $menu_name }}
{{ end }}

<form class="docs-search d-flex align-items-center">
  <button class="btn docs-toggle d-md-none p-0 mr-3" type="button" data-toggle="collapse" data-target="#docs-nav" aria-controls="docs-nav" aria-expanded="false" aria-label="Toggle section navigation">
    <span><i class="fas fa-bars"></i></span>
  </button>

  {{ if eq site.Params.search.engine 1 }}
  <input name="q" type="search" class="form-control" placeholder="{{ i18n "search_placeholder" }}" autocomplete="off">
  {{ end }}
</form>

<nav class="collapse docs-links" id="docs-nav">
  {{ with (index site.Menus $menu_name) }}

  {{/* Enable Updates layout to order by descending version name. */}}
  {{ $menu := (index site.Menus $menu_name) }}
  {{ if eq ($.Scratch.Get "docs_menu_sort" | default "weight") "name_reverse" }}
    {{ $menu = sort $menu "Title" "desc" }}
  {{ else }}
    {{ $menu = $menu.ByWeight }}
  {{end}}

  {{ range $menu }}
  <div class="docs-toc-item{{ if $current_page.IsMenuCurrent $menu_name . }} active{{ end }}">
    <a class="docs-toc-link" {{ if .URL }}href="{{ .URL }}"{{else if .HasChildren }}href="{{ (index .Children 0).URL }}"{{end}}>{{ .Name }}</a>

    {{- if .HasChildren }}
    <ul class="nav docs-sidenav">
      {{ range .Children }}
      <li {{ if $current_page.IsMenuCurrent $menu_name . }}class="active"{{ end }}>
        <a href="{{ .URL }}">{{ .Name }}</a>
      </li>

      {{ end }}
    </ul>
    {{ end }}

  </div>
  {{ end }}
  {{ end }}
</nav>
